﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mimoza.Common;

namespace Mimoza.ObjectLayer
{
    public class UpdateRepository
    {
        private OL ol;
        public UpdateRepository(OL ol)
        {
            this.ol = ol;
            this.ol.DBLayer.DBUpdateRepository.OnObjectChanged += DBObjectChangedEventHandler;
        }

        public event EventHandler<ObjectLayer_ObjectChangedEventArgs> OnObjectChanged;
        private void RaiseOnObjectChanged(ObjectLayer_ObjectChangedEventArgs e)
        {
            EventHandler<ObjectLayer_ObjectChangedEventArgs> handler = OnObjectChanged;

            if (handler != null)
            {
                handler(this, e);
            }
        }
        void DBObjectChangedEventHandler(object sender, DBObjectChangedEventArgs e)
        {
            ObjectLayerEventType type = ObjectLayerEventType.ObjectInserted;
            Object obj;
            switch (e.EventType)
            {
                case DBEventType.ObjectInserted:
                    type = ObjectLayerEventType.ObjectInserted;
                    break;
                case DBEventType.ObjectUpdated:
                    type = ObjectLayerEventType.ObjectUpdated;
                    break;
                case DBEventType.ObjectDeleted:
                    type = ObjectLayerEventType.ObjectDeleted;
                    break;
            }

            obj = this.CreateUpdate(e.Object as UpdateRecord);
            ObjectLayer_ObjectChangedEventArgs args = new ObjectLayer_ObjectChangedEventArgs(obj, type);

            RaiseOnObjectChanged(args);
        }

        public Update CreateUpdate()
        {
            return new Update(ol);
        }
        public Update CreateUpdate(Guid UpdateID)
        {
            UpdateRecord record = new UpdateRecord();
            record.ID = UpdateID;
            return new Update(ol, record);
        }
        public Update CreateUpdate(UpdateRecord record)
        {
            return new Update(ol, record);
        }

        public Update GetUpdateByID(Guid ID)
        {
            UpdateRecord record = ol.DBLayer.DBUpdateRepository.GetUpdate(ID);
            if (record == null) return null;
            return new Update(ol, record);
        }

        public IEnumerable<Update> GetUpdates()
        {
            foreach (UpdateRecord record in ol.DBLayer.DBUpdateRepository.GetUpdates())
            {
                yield return new Update(ol, record);
            }
        }

        public void Add(Update update)
        {
            ol.DBLayer.DBUpdateRepository.Insert(update.Record);
        }
        public void Delete(Update update)
        {
            ol.DBLayer.DBUpdateRepository.Delete(update.Record);
        }
        public void Update(ref Update update)
        {
            ol.DBLayer.DBUpdateRepository.Update(update.Record);
        }
    }
}
